2d ball collision code problem XNA, over accelerated balls and stick together sometimes. help please? [closed]
Posted
by
Sivan
on Game Development
See other posts from Game Development
or by Sivan
Published on 2012-09-13T22:24:53Z
Indexed on
2012/09/14
15:50 UTC
Read the original article
Hit count: 255
public static void Collision(Ball ball1, Ball ball2)
{
Vector3 x = new Vector3((ball1.BallPosition.X - ball2.BallPosition.X), (ball1.BallPosition.Y - ball2.BallPosition.Y), 0);
x.Normalize();
Vector3 v1 = new Vector3(ball1.Speed, 0);
float x1 = Vector3.Dot(x, v1);
Vector3 v1x = x * x1;
Vector3 v1y = v1 - v1x;
x = -x;
Vector3 v2 = new Vector3(ball2.Speed, 0);
float x2 = Vector3.Dot(x, v2);
Vector3 v2x = x * x2;
Vector3 v2y = v2 - v2x;
float m1 = 12, m2 = 4;
float combinedMass = m1 + m2;
Vector3 newVelA = (v1x * ((m1 - m2) / combinedMass)) + (v2x * ((2f * m2) / combinedMass)) + v1y;
Vector3 newVelB = (v1x * ((2f * m1) / combinedMass)) + (v2x * ((m2 - m1) / combinedMass)) + v2y;
ball1.Speed = new Vector2(newVelA.X, newVelA.Y);
ball2.Speed = new Vector2(newVelB.X,newVelB.Y );
}
© Game Development or respective owner